package com.chexiehui.jc.base;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;

/**
 * @描述: BaseMvpActivity
 * @作者: 宋俊 SongJun
 * @时间: 2016/11/10 12:13
 * @版权: 云腾科技
 * @官网: http://www.gzytkj.net/
 */
public abstract class BaseMvpFragment<P extends BasePresenter> extends BaseFragment {

    protected P mvpPresenter;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mvpPresenter = createPresenter();
        super.onViewCreated(view, savedInstanceState);
    }

    protected abstract P createPresenter();

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (mvpPresenter == null){
            mvpPresenter = createPresenter();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if(isCancelRequestOnClickBack()){
                if(mSVProgressHUD != null && mSVProgressHUD.isShowing()){
                    mvpPresenter.cleanAllRequest();
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //解绑
        if (mvpPresenter != null) {
            mvpPresenter.detachView();
            mvpPresenter=null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
